package graph.group.zhangtao.guider.utils;

import java.io.BufferedInputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Scanner;

/**
 * Created by Me on 2017/12/24.
 */

public class MySettingsAndTools {
    //文件路径
    public static String getSourcePath() {
        return sourcePath;
    }

    public static void setSourcePath(String sourcePath) {
        sourcePath = sourcePath;
    }

    private static String sourcePath = "http://zhangtao.group:8080/files/";

    //index路径
    public static String getIndexPath() {
        return indexPath;
    }

    public static void setIndexPath(String indexPath) {
        indexPath = indexPath;
    }

    private static String indexPath = "http://zhangtao.group:8190/";

    //错误
    public static String getWRONG() {
        return WRONG;
    }

    private static final String WRONG = "wrong";

    public static String getNOPICTURE() {
        return NOPICTURE;
    }

    private static final String NOPICTURE="http://zhangtao.group:8080/files/%E6%AD%A6%E5%A4%A7/1509004414110/1509004484892.jpg";

    public static String getNOCONTENT() {
        return NOCONTENT;
    }

    private static final String NOCONTENT="没有文字说明啊！";


    /**
     * 获取text的html
     *
     * @param path
     * @return
     * @throws Exception
     */
    public static String getHtml(String path) throws Exception {
        // 通过网络地址创建URL对象
        URL url = new URL(path);
        // 根据URL
        // 打开连接，URL.openConnection函数会根据URL的类型，返回不同的URLConnection子类的对象，这里URL是一个http，因此实际返回的是HttpURLConnection
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        // 设定URL的请求类别，有POST、GET 两类
        conn.setRequestMethod("GET");
        //设置从主机读取数据超时（单位：毫秒）
        conn.setConnectTimeout(5000);
        //设置连接主机超时（单位：毫秒）
        conn.setReadTimeout(5000);
//        byte[] data = readInputStream(inStream);
//        // 是用指定的字符集解码指定的字节数组构造一个新的字符串
//        String html = new String(data, "utf-8");
        StringBuilder stringBuilder = new StringBuilder();
        //        // 得到html的二进制数据

        // 通过打开的连接读取的输入流,获取html数据
        InputStream inStream = conn.getInputStream();
        Scanner scanner = new Scanner(
                new BufferedInputStream(
                        inStream
                )
        );
        while (scanner.hasNextLine()) {
            stringBuilder.append(scanner.nextLine());
        }
        return stringBuilder.toString();
    }
}
